#ifndef DATABASE_SERVICE_H
#define DATABASE_SERVICE_H
#include "jservice_runner.h"
#include "jservice.h"
#include "db_proto.h"
#include "jmsg.h"
#include <mysql.h>
#include <string>
class NetworkService;
enum DatabaseCommand {
	kDatabaseCommandSaveUserInfo,
	kDatabaseCommandQueryDataInfo,
};
class DatabaseService : public JService
{
public:
	DatabaseService(void);
	~DatabaseService(void);

public:
	virtual void onStart(void* startArgs);
	virtual void onMsg(JService* service, int msgId, int sessionId, void* args);
	template<class T>
	void sendConnMsg(int connId, T& msg) {
		JMsgWriter writer;
		msg.encode(m_proto, &writer);
		m_networkService->sendConnectionData(connId, writer.getBuffer(), writer.getBufferLen());
	}
private:
	void procGetPlayerInfoRequest(int sessionId, DBMsgPlayerInfoRequest* req);
	void procAddRegisterPlayerRequest(int sessionId, DBMsgAddRegisterPlayerRequest* req);
	void procActivatePlayerRequest(int sessionId, DBMsgActivatePlayerRequest* req);
	void procSavePlayerInfo(int sessionId, DBMsgSavePlayerInfo* req);
	MYSQL_STMT* createStmt(const std::string&  sql);
	void bindString(MYSQL_BIND* bind, const char* str);
	void bindBool(MYSQL_BIND* bind, const bool& value);
	//bool addPlayer()
private:
	MYSQL* m_conn;
	NetworkService* m_networkService;
	JMsgProto* m_proto;
};
#endif
